<template>
  <!--  -->
  <!-- 自定义组件：文件预览整合 -->
  <!--  -->

  <!-- 图片预览组件 -->
  <PreviewImage ref="imageViewRef" :imageList="[imageUrl]" v-if="fileInfo.fileCategory == 3"></PreviewImage>
  
  <!-- 其它文件预览组件 -->
  <Window
    :show="windowShow"
    @close="closeWindow"
    :width="fileInfo.fileCategory == 1 ? 1000 : 550"
    :title="fileInfo.fileName"
    :align="fileInfo.fileCategory == 1 ? 'center' : 'top'"
    v-else>
        <!-- 视频预览 -->
        <PreviewVideo :url="url" v-if="fileInfo.fileCategory == 1"></PreviewVideo>
        <!-- docx文档预览 -->
        <PreviewDoc :url="url" v-if="fileInfo.fileType == 5"></PreviewDoc>
        <!-- Excel文档预览 -->
        <PreviewExcel :url="url" v-if="fileInfo.fileType == 6"></PreviewExcel>
        <!-- pdf文档预览 -->
        <PreviewPdf :url="url" v-if="fileInfo.fileType == 4"></PreviewPdf>
        <!-- txt文本预览 -->
        <PreviewTxt :url="url" v-if="fileInfo.fileType ==  7 || fileInfo.fileType == 8"></PreviewTxt>
        <!-- 音频播放 -->
        <PreviewMusic :url="url" :fileName="fileInfo.fileName" v-if="fileInfo.fileCategory == 2"></PreviewMusic>
        <!-- 文件下载 -->
        <PreviewDownload 
          :createDownloadUrl="createDownloadUrl" 
          :downloadUrl="downloadUrl"
          :fileInfo="fileInfo"
          v-if="fileInfo.fileCategory == 5 && fileInfo.fileType != 8">
        </PreviewDownload>
        
  </Window>

</template>

<script  setup>
import { ref, reactive, getCurrentInstance, nextTick, onMounted, watch, computed } from "vue";
import PreviewImage from "./PreviewImage.vue";
import PreviewVideo from "@/components/preview/PreviewVideo.vue";
import PreviewDoc from "./PreviewDoc.vue";
import PreviewExcel from "./PreviewExcel.vue";
import PreviewPdf from "./PreviewPdf.vue";
import PreviewTxt from "./PreviewTxt.vue";
import PreviewMusic from "./PreviewMusic.vue";
import PreviewDownload from "./PreviewDownload.vue";
const { proxy } = getCurrentInstance();

const FILE_URL_MAP = {
    //普通用户
    0: {
        fileUrl: "/file/getFile",                      //获取文件
        videoUrl: "/file/ts/getVideoInfo",             //获取视频
        createDownloadUrl: "/file/createDownloadUrl",  //创建下载链接
        downloadUrl: "/api/file/download",             //文件下载  
    },
    //管理员
    1: {
        fileUrl: "/admin/getFile",
        videoUrl: "/admin/ts/getVideoInfo",
        createDownloadUrl: "/admin/createDownloadUrl",
        downloadUrl: "/api/admin/download",
    },
    //外部分享
    2: {
        fileUrl: "/showShare/getFile",
        videoUrl: "/showShare/ts/getVideoInfo",
        createDownloadUrl: "/showShare/createDownloadUrl",
        downloadUrl: "/api/showShare/download",
    },
};

//要预览的图片路径
const imageUrl = computed(() => {
    //proxy.globalInfo.imageUrl在main.js定义
    //replaceAll方法：将 _. 替换成 .
    return proxy.globalInfo.imageUrl + fileInfo.value.fileCover.replaceAll("_.", "."); 
});


const fileInfo = ref({});            //要预览的文件对象
const url = ref({});                 //预览请求
const createDownloadUrl = ref(null); //创建下载（获得code）
const downloadUrl = ref(null);       //文件下载 
const imageViewRef = ref();          //PreviewImage.vue组件对象

//预览组件选择 -- 由父组件Main.vue的preview方法调用
const showPreview = (data, showPart) => {

    fileInfo.value = data;          //获取对象

    if (data.fileCategory == 3) {   //图片预览
        nextTick(() => {
            imageViewRef.value.show(0);
        });
    } else {
        windowShow.value = true;                     //打开Window.vue组件弹窗
        let _url = FILE_URL_MAP[showPart].fileUrl;   //默认预览文本文件
        let _createDownloadUrl = FILE_URL_MAP[showPart].createDownloadUrl;  
        let _downloadUrl = FILE_URL_MAP[showPart].downloadUrl;
        if (data.fileCategory == 1) {
            _url = FILE_URL_MAP[showPart].videoUrl;  //判断文件类型为视频
        }
        if (showPart == 0) {        //普通用户操作
            _url = _url + "/" + data.fileId;          //拼接成完整的路径
            _createDownloadUrl = _createDownloadUrl + "/" + data.fileId;
        } else if (showPart == 1){  //管理员操作
            _url = _url + "/" + data.userId + "/" + data.fileId;          //拼接成完整的路径
            _createDownloadUrl = _createDownloadUrl + "/" + data.userId + "/" + data.fileId;
        }
        url.value = _url;
        createDownloadUrl.value = _createDownloadUrl;
        downloadUrl.value = _downloadUrl;
    }
};
defineExpose({ showPreview });    //对外暴露


const windowShow = ref(false);     //Window组件的预览窗口是否打开
//关闭Window组件的预览窗口
const closeWindow = () => {
    windowShow.value = false;
}

</script>

<style>

</style>